package com.householditemtwo.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.householditemtwo.admin.dao.NewsTypeDao;
import com.householditemtwo.common.entity.News;
import com.householditemtwo.common.entity.Newstype;
import com.householditemtwo.common.utils.BaseServlet;
import com.householditemtwo.common.utils.Result;
import com.mysql.jdbc.StringUtils;

/**
 * 新闻Servlet 
 */
@WebServlet("/admin/newstype")
public class NewsTypeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	//创建dao
	NewsTypeDao dao=new NewsTypeDao();
	
	
	/**
	 * 新闻列表查询所有的新闻和条件查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接收页面传的条件查询数据
		String proname=request.getParameter("newstypename");
		
		//接收页面传过来的分页值
		//当前是第几页，如果没传就默认第一页
		int page=StringUtils.isNullOrEmpty(request.getParameter("page"))? 1 : Integer.parseInt(request.getParameter("page"));
		//每页显示几条
		int pageSize=StringUtils.isNullOrEmpty(request.getParameter("limit"))? 1 : Integer.parseInt(request.getParameter("limit"));
		
		//调用dao
		int total=dao.count(proname);
		List<Newstype> list=dao.selectAllNews(page,pageSize,proname);
		//把数据传到页面(必须传给也页面json格式的数据)
		PrintWriter out=response.getWriter();
		out.write(Result.toClient(0, "成功", total, list));
		out.flush();
		out.close();
	}
	
	/**
	 * 修改新闻
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收页面传的产品详情，并且封装到实体类里
		Newstype p=new Newstype();
		p.setNewstypeid(Integer.parseInt(request.getParameter("newstypeid")));
		p.setNewstypename(request.getParameter("newstypename"));
		
		
	
		//调用DAO
		int res=dao.update(p);
		
		
		//把数据传到页面(必须传给也页面json格式的数据)
		PrintWriter out=response.getWriter();
		out.write(Result.toClient(res>0 ? 0 : 1,res>0 ? "修改成功" : "修改失败", null, null));
		out.flush();
		out.close();
		
		
	}
	
	/**新增新闻
	 * 修改cahnpin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收页面传的产品详情，并且封装到实体类里
		Newstype p=new Newstype();
		p.setNewstypename(request.getParameter("newstypename"));
				//调用DAO
				int res=dao.insert(p);
				
				
				//把数据传到页面(必须传给也页面json格式的数据)
				PrintWriter out=response.getWriter();
				out.write(Result.toClient(res>0 ? 0 : 1,res>0 ? "新增成功" : "新增失败", null, null));
				out.flush();
				out.close();
				
	
	
	
	}
	
	/**删除新闻
	 * 修改cahnpin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收页面传的产品详情，并且封装到实体类里
		int id=	Integer.parseInt(request.getParameter("newstypeid"));
		
		
		
		//调用DAO
		int res=dao.delete(id);
		
		
		//把数据传到页面(必须传给也页面json格式的数据)
		PrintWriter out=response.getWriter();
		out.write(Result.toClient(res>0 ? 0 : 1,res>0 ? "删除成功" : "删除失败", null, null));
		out.flush();
		out.close();
		
	
	
	
	}
}
